package com.taotao.strategy.strategy.controller;


import com.taotao.strategy.strategy.entity.PaymentChannelEntity;
import com.taotao.strategy.strategy.factory.StrategyFactory;
import com.taotao.strategy.strategy.mapper.PaymentChannelMapper;
import com.taotao.strategy.strategy.service.PayStrategy;
import com.taotao.strategy.strategy.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjin
 * @title: PayContextService
 * @projectName mayikt_ strategy
 * @description: TODO
 * @date 2022/6/12 0012 19:30
 */
@RestController
public class PayContextService {
    public StrategyFactory strategyFactory=new StrategyFactory();
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;
    @RequestMapping("/toPayHtml")
    public String toPayHtml(String payCode){
      if(StringUtils.isEmpty(payCode)){
          return "payCode is null";
      }
      //根据paycode名称从strategyFactory获取具体的Strategy策略
       // PayStrategy payStrategy= strategyFactory.getPayStrategy(payCode);
        //使用SpringUtils從IOC容器里获取bean对象
      //
        PaymentChannelEntity paymentChannelEntity = paymentChannelMapper.getPaymentChannel(payCode);
        if (paymentChannelEntity == null) {
            return "payCode error  or 渠道已经关闭 ";
        }
        String strategyBeanId = paymentChannelEntity.getStrategyBeanId();
        PayStrategy payStrategy= SpringUtils.getBean(strategyBeanId, PayStrategy.class);
       if(payStrategy ==null){
           return "not found payStrategy";
       }
       //获取具体的payStrategy调用toPayHtml
        return payStrategy.toPayHtml();
    }
}
